//
//  StuHeaderViewController.m
//  hellomui
//
//  Created by zhangtao on 15/9/23.
//  Copyright © 2015年 zt.td. All rights reserved.
//

#import "StuHeaderViewController.h"
#import <QAserviceAskSDK/AskHandle.h>

@interface StuHeaderViewController ()<UIWebViewDelegate>
//@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *loading;
@property (strong, nonatomic) IBOutlet UIView *noNetStatusIV;

@property (strong, nonatomic) IBOutlet UIButton *loadBtn;
@property (weak, nonatomic) IBOutlet UIWebView *web;

@end

@implementation StuHeaderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIFont systemFontOfSize:20.0],UITextAttributeFont,nil]];

    
    [self netStatus];
    self.loadBtn.layer.borderWidth = 2;
    self.loadBtn.layer.borderColor = [[UIColor blueColor]CGColor];
    
    UIButton *sender = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
    sender.backgroundColor = [UIColor orangeColor];
    [sender addTarget:self action:@selector(clickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sender];
}

- (void)clickAction {
    [[AskHandle sharedInstance] setUserId:@"0"];
    [[AskHandle sharedInstance] setAppKey:@"61f38469caa7bbdabacf56bfd9ab5012" appSecret:@"5649a6701198e"];
    [[AskHandle sharedInstance]setActionSegue:ActionSeguePush];
    [[AskHandle sharedInstance]setAskType:AskTypeProfessional];
//    dispatch_async(dispatch_get_main_queue(), ^{

    [[AskHandle sharedInstance] openAskViewControllerFromVC:self];
//    });
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
    [self netStatus];
    
    _web.delegate = self;
    
 
        [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://182.92.117.144:8080/study-manager/question"]]];
    
}

-(void)netStatus{
    
    NSString *status = [[NSUserDefaults standardUserDefaults]valueForKey:@"netStatus"];
    if ([status isEqualToString:@"0"] ||
        [status isEqualToString:@"-1"]) {
        _noNetStatusIV.hidden = NO;
    }else{
    _noNetStatusIV.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loadBtn:(id)sender {
    
    
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"webViewDidStartLoad");
//    _loading.hidden = NO;
//    [_loading setCenter:CGPointMake(MAIN_WIDTH/2+10,MAIN_HEIGHT/2)];
//    _loading.backgroundColor = [UIColor grayColor];
//    [_loading setTintColor:[UIColor grayColor]];
//    _loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
//    [self.view addSubview:_loading];
//    [_loading startAnimating];
    [[MBProgressController sharedInstance]showWithText:@"正在加载"];

    
}
- (void)webViewDidFinishLoad:(UIWebView *)web{
    NSLog(@"webViewDidFinishLoad");
    self.navigationItem.title=[web stringByEvaluatingJavaScriptFromString:@"document.title"];//获取当前页面的title
//    [_loading stopAnimating];
//    _loading.hidden = YES;
    [(UIScrollView *)[[_web subviews] objectAtIndex:0] setBounces:NO];

    [[MBProgressController sharedInstance] hide];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
